{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Testing with [pytest](https://docs.pytest.org/en/latest/) - part 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Why to write tests?\n",
    "* Who wants to perform manual testing?\n",
    "* When you fix a bug or add a new feature, tests are a way to verify that you did not break anything on the way\n",
    "* If you have clear requirements, you can have matching test(s) for each requirement\n",
    "* You don't have to be afraid of refactoring\n",
    "* Tests document your implementation - they show other people use cases of your implementation\n",
    "* This list is endless..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [Test-driven development](https://en.wikipedia.org/wiki/Test-driven_development) aka TDD\n",
    "In short, the basic idea of TDD is to write tests before writing the actual implementation. Maybe the most significant benefit of the approach is that the developer focuses on writing tests which match with what the program should do. Whereas if the tests are written after the actual implementation, there is a high risk for rushing tests which just show green light for the already written logic.\n",
    "\n",
    "Tests are first class citizens in modern, agile software development, which is why it's important to start thinking TDD early during your Python learning path. \n",
    "\n",
    "The workflow of TDD can be summarized as follows:\n",
    "1. Add a test case(s) for the change / feature / bug fix you are going to implement\n",
    "2. Run all tests and check that the new one fails\n",
    "3. Implement required changes\n",
    "4. Run tests and verify that all pass\n",
    "5. Refactor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Running pytest inside notebooks\n",
    "These are the steps required to run pytest inside Jupyter cells. You can copy the content of this cell to the top of your notebook which contains tests."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's make sure pytest and ipytest packages are installed\n",
    "# ipytest is required for running pytest inside Jupyter notebooks\n",
    "import sys\n",
    "!{sys.executable} -m pip install pytest\n",
    "!{sys.executable} -m pip install ipytest\n",
    "\n",
    "import ipytest.magics\n",
    "import pytest\n",
    "\n",
    "# Filename has to be set explicitly for ipytest \n",
    "__file__ = 'testing1.ipynb'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `pytest` test cases\n",
    "Let's consider we have a function called `sum_of_three_numbers` for which we want to write a test."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This would be in your e.g. implementation.py\n",
    "def sum_of_three_numbers(num1, num2, num3):\n",
    "    return num1 + num2 + num3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Pytest test cases are actually quite similar as you have already seen in the exercises. Most of the exercises are structured like pytest test cases by dividing each exercise into three cells:\n",
    "1. Setup the variables used in the test\n",
    "2. Your implementation\n",
    "3. Verify that your implementation does what is wanted by using assertions\n",
    "\n",
    "See the example test case below to see the similarities between the exercises and common structure of test cases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%run_pytest[clean]\n",
    "# Mention this at the top of cells which contain test(s)\n",
    "# This is only required for running pytest in Jupyter notebooks\n",
    "\n",
    "\n",
    "# This would be in your test_implementation.py\n",
    "def test_sum_of_three_numbers():\n",
    "    # 1. Setup the variables used in the test\n",
    "    num1 = 2\n",
    "    num2 = 3\n",
    "    num3 = 5\n",
    "    \n",
    "    # 2. Call the functionality you want to test\n",
    "    result = sum_of_three_numbers(num1, num2, num3)\n",
    "    \n",
    "    # 3. Verify that the outcome is expected\n",
    "    assert result == 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now go ahead and change the line `assert result == 10` such that the assertion fails to see the output of a failed test."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
